package com.server.web.controller.pinggu;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.server.common.annotation.Log;
import com.server.common.core.controller.BaseController;
import com.server.common.core.domain.AjaxResult;
import com.server.common.core.page.TableDataInfo;
import com.server.common.enums.BusinessType;
import com.server.common.utils.poi.ExcelUtil;
import com.server.pinggu.domain.SenUserdata;
import com.server.pinggu.service.ISenUserdataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ltao
 * @date 2022-10-23
 */
@RestController
@RequestMapping("/sen/userdata")
public class SenUserdataController extends BaseController
{
    @Autowired
    private ISenUserdataService senUserdataService;

    /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('sen:userdata:list')")
    @GetMapping("/list")
    public TableDataInfo list(SenUserdata senUserdata)
    {
        startPage();
        List<SenUserdata> list = senUserdataService.queryList(senUserdata);
        return getDataTable(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('sen:userdata:export')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SenUserdata senUserdata)
    {
        List<SenUserdata> list = senUserdataService.queryList(senUserdata);
        ExcelUtil<SenUserdata> util = new ExcelUtil<SenUserdata>(SenUserdata.class);
        return util.exportExcel(list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('sen:userdata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(senUserdataService.getById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('sen:userdata:add')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SenUserdata senUserdata)
    {
        return toAjax(senUserdataService.save(senUserdata));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('sen:userdata:edit')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SenUserdata senUserdata)
    {
        return toAjax(senUserdataService.updateById(senUserdata));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('sen:userdata:remove')")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(senUserdataService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("setLuruYuan/{ids}")
    public AjaxResult setLuruYuan(@PathVariable Long[] ids)
    {
        List<SenUserdata> list = senUserdataService.list(new LambdaUpdateWrapper<SenUserdata>().in(SenUserdata::getId, ids));
        if (CollectionUtil.isNotEmpty(list)){
            List<SenUserdata> collect = list.stream().map(a -> {
                SenUserdata senUserdata = new SenUserdata();
                senUserdata.setId(a.getId());
                senUserdata.setIsLuru(a.getIsLuru() == 0 ? 1 : 0);
                return senUserdata;
            }).collect(Collectors.toList());
            boolean update = senUserdataService.updateBatchById(collect);
            return toAjax(update);
        }
        return toAjax(true);
    }
    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("setAdmin/{ids}")
    public AjaxResult setAdmin(@PathVariable Long[] ids)
    {
        List<SenUserdata> list = senUserdataService.list(new LambdaUpdateWrapper<SenUserdata>().in(SenUserdata::getId, ids));
        if (CollectionUtil.isNotEmpty(list)){
            List<SenUserdata> collect = list.stream().map(a -> {
                SenUserdata senUserdata = new SenUserdata();
                senUserdata.setId(a.getId());
                senUserdata.setIsAdmin(a.getIsAdmin() == 0 ? 1 : 0);
                return senUserdata;
            }).collect(Collectors.toList());
            boolean update = senUserdataService.updateBatchById(collect);
            return toAjax(update);
        }
        return toAjax(true);
    }
}
